<?php

namespace app\admin\service;


use app\admin\model\RechargeOrder;
use think\facade\Db;

class RechargeOrderService extends BaseService
{
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/11/14
     * RoleService constructor.
     */
    public function __construct()
    {
        $this->model = new RechargeOrder();
    }

    public function getList()
    {
        $data = request()->param();


        $where = [];
        if (isset($data['mobile'])) {
            if ($data['mobile'] != "") {
                $where[] = ['u.mobile', '=', trim($data['mobile'])];
            }
        }
        if (isset($data['add_time'])) {
            if ($data['add_time'] != "") {
                $where[] = ['o.create_time', '>=', strtotime($data['add_time'])];
            }
        }
        if (isset($data['end_time'])) {
            if ($data['end_time'] != "") {
                $where[] = ['o.create_time', '<=', strtotime($data['end_time']) + 86400];
            }
        }
        if (isset($data['store_id'])) {
            if ($data['store_id'] != "") {
                $where[] = ['o.store_id', '=', $data['store_id']];
            }
        }
        $where[] = ['o.mark', '=', 1];
        $where[] = ['o.status', '=', 1];


        $list = Db::name('customer_recharge_order')
            ->alias('o')
            ->join('mini_room_list m', 'm.id=o.store_id')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile')
            ->where($where)->order('o.create_time desc')->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $info) {
            $course = Db::name('customer_recharge')->where('id', $info['level_id'])->find();
            $store = Db::name('mini_room_list')->where('id', $info['store_id'])->find();
            $info['room_name'] = $store['room_name'];

            $info['name'] = $course['title'];

            $list[$i] =  $info;
        }
        $count = Db::name('customer_recharge_order')
            ->alias('o')
            ->join('mini_room_list m', 'm.id=o.store_id')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile')
            ->where($where)
            ->count();



        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}